<?php
namespace app\controller;

use think\exception\ValidateException;

class Install extends Base
{
    // 初始化
    protected function initialize()
    {
        parent::initialize();
        $this->logic = new \app\logic\Install();
    }

    /**
     * 一键安装
     */
    public function index()
    {
        $is_install = $this->logic->isInstall();
        if(request()->isAjax()) {
            if($is_install) {
                return $this->jsonReturn(config('status.fail'), "非法操作");
            }
            $param = request()->param(['host', 'dbuser', 'dbpass', 'dbname', 'port', 'prefix', 'username', 'password'], [], 'trim,strip_tags');
            // 数据验证
            try {
                validate(\app\validate\Install::class)->check($param);
            } catch(ValidateException $e) {
                return $this->jsonReturn(config('status.verify'), $e->getError());
            }

            return $this->jsonReturn($this->logic->install($param));
        }
        if($is_install) {
            return redirect("/");
        }
        return view();
    }
}